package com.example.zhilongzhang.constructionpayapp.activity;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhilongzhang.constructionpayapp.R;
import com.example.zhilongzhang.constructionpayapp.base.BaseActivity;
import com.example.zhilongzhang.constructionpayapp.bean.StringBean;
import com.example.zhilongzhang.constructionpayapp.bean.UserInfoBean;
import com.example.zhilongzhang.constructionpayapp.http.HttpMethod;
import com.example.zhilongzhang.constructionpayapp.image.GlideImageLoader;
import com.example.zhilongzhang.constructionpayapp.method.bluetooth.BluetoothUtil;
import com.example.zhilongzhang.constructionpayapp.method.bluetooth.PrintUtil;
import com.example.zhilongzhang.constructionpayapp.method.bluetooth.ToothMethod;
import com.example.zhilongzhang.constructionpayapp.util.SpUtil;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//轮播图，图标
public class MainActivity extends BaseActivity {
    Banner banner;
    List<Integer> mImgList;
    List<String> mImgList2=new ArrayList<>();
    TextView tvNew;
    ImageView imgHeat;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        banner = findViewById(R.id.banner);
        tvNew = findViewById(R.id.tv_new);
        imgHeat = findViewById(R.id.img_heat);
        mImgList = new ArrayList<>();

        if (TextUtils.isEmpty(SpUtil.getListImg())){
            mImgList.add(R.mipmap.jianse1);
            mImgList.add(R.mipmap.jianse2);
            mImgList.add(R.mipmap.jianse3);
            mImgList.add(R.mipmap.jianse4);
        }else {
            String[] imgLenth=SpUtil.getListImg().split(",");
            for (int i = 0; i < imgLenth.length; i++) {
                mImgList2.add(imgLenth[i]);

            }
        }
        if (!TextUtils.isEmpty(SpUtil.getType())){
            Log.e("type","type="+SpUtil.getType());
            switch (SpUtil.getType()){
                case "2":
                    Glide.with(this).load(R.mipmap.water_logo).into(imgHeat);
                    tvNew.setText("水费");
                    break;
                case "3":
                    Glide.with(this).load(R.mipmap.heat_logo).into(imgHeat);
                    tvNew.setText("燃气费");
                    break;
                case "1":
                    Glide.with(this).load(R.mipmap.new_logo).into(imgHeat);
                    tvNew.setText("暖气费");
                    break;
                default:
                    Glide.with(this).load(R.mipmap.heat_logo).into(imgHeat);
                    tvNew.setText("其他");
                    break;
            }
        }
        setBanner();
        initData();
    }
    private void initData() {
        findViewById(R.id.lin_money).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SetMonetActivity.class));
            }
        });
        findViewById(R.id.lin_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, RecordActivity.class));
            }
        });
        findViewById(R.id.lin_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, UserInfoActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.lin_heat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });
        findViewById(R.id.lin_stat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, StatisticalActivity.class));
            }
        });
        findViewById(R.id.lin_set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, OptionActivity.class));
            }
        });
    }

    private void setBanner() {
        //设置banner样式
//       banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        if (TextUtils.isEmpty(SpUtil.getListImg())){
            banner.setImages(mImgList);
        }else {
            Log.e("type","type="+mImgList2.get(0));
            Log.e("type","type="+mImgList2.get(1));
            banner.setImages(mImgList2);
        }
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
//      //设置标题集合（当banner样式有显示title时）
//      banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    public static void setViewSize(View view, int width, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    @Override
    public void onResume() {
        super.onResume();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    public void onPause() {
        super.onPause();
        //结束轮播
        banner.stopAutoPlay();
    }

    private void getData() {
        HttpMethod.security(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                if (response == null) {
                    return;
                }
                UserInfoBean bean = new Gson().fromJson(response, UserInfoBean.class);
                if (bean.getDept() != null) {
                    SpUtil.setName(bean.getRealName());
                    SpUtil.setShopName(bean.getDept().getDeptName());
                    SpUtil.setShopSearUrl(String.valueOf(bean.getDept().getDepReqUrl()));
                    SpUtil.setShopAddUrl(String.valueOf(bean.getDept().getDepSaveUrl()));
                    SpUtil.setQRImgUrl(String.valueOf(bean.getDept().getQrReqUrl()));
                    SpUtil.setQRPayUrl(String.valueOf(bean.getDept().getQrSaveUrl()));
                    SpUtil.setShopId(String.valueOf(bean.getDept().getId()));

                }
                if (bean.getTel() != null) {
                    SpUtil.setTel(bean.getTel());
                }
                SpUtil.setUserId(String.valueOf(bean.getId()));

            }
        });
    }

    private long firstTime = 0;
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    firstTime = secondTime;
                    return true;
                } else {
                    finish();
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

}
